<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1.0,user-scalable=0,maximum-scale=1.0" />
    <script src="http://g.tbcdn.cn/mtb/lib-flexible/0.3.4/??flexible_css.js,flexible.js"></script>
    <link rel="stylesheet" type="text/css" href="css/reset.css"/>
    <link rel="stylesheet" type="text/css" href="css/style.css"/>
    <script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js" type="text/javascript" charset="utf-8"></script>
    <script src="js/public.js" type="text/javascript" charset="utf-8"></script>
    <link rel="stylesheet" type="text/css" href="css/LArea.min.css"/>
    <title>邮寄地址</title>
</head>
<body>
	<div id="newAdd">
		<div class="addHeader">
			<span class="back"></span>
			<span class="title">新增地址</span>
			<span class="addBtn" onclick="confirm()">确定</span>
		</div>
		<div class="newList">
			<div class="list">
				<span class="listTitle">公司单位</span>
				<input type="text" id="companyName" name="companyName" class="name" value="" placeholder="请填写您的公司名称"/>
			</div>
			<div class="list">
				<span class="listTitle">收件人姓名</span>
				<input type="text" id="name" name="name" class="name" value="" placeholder="请填写您的收件人姓名"/>
			</div>
			<div class="list region">
				<span class="listTitle">所在地区</span>
				<input id="region" type="text" name="region" readonly="" placeholder="请选择城市"  value=""/>
			</div>
			<div class="list">
				<span class="listTitle">详细地址</span>
				<textarea id="listStreet" name="listStreet" class="listStreet" placeholder="请填写您的详细地址"></textarea>
			</div>
			<div class="tips">
				<i>街道地址不能为空</i>
			</div>
			<div class="list">
				<span class="listTitle">邮政编码</span>
				<input type="text" name="ZipCode" class="ZipCode" value="" placeholder="请填写您的邮政编码"/>
			</div>
			<div class="tips">
				<i>邮编号码必须为6位数字</i>
			</div>
			<div class="list">
				<span class="listTitle">手机号</span>
				<input type="text" name="mobile" class="mobile" value="" placeholder="请填写您的手机号"/>
			</div>
			<div class="tips">
				<i>手机号码长度为11位数字</i>
			</div>
			<!--<div class="list">-->
				<!--<span class="listTitle">设为默认</span>-->
				<!--<div class="check">-->
					<!--<span class="circle"></span>-->
				<!--</div>-->
			<!--</div>-->
		</div>
	</div>
	<input type="hidden" id="value1" value="" />
<script src="js/LArea.min.js" type="text/javascript" charset="utf-8"></script>
<script src="js/LAreaData1.js" type="text/javascript" charset="utf-8"></script>
<!-- <script src="js/LAreaData2.js" type="text/javascript" charset="utf-8"></script> -->
	<script>
		$(function () {
            var addId = sessionStorage.getItem('addId');
            if (sessionStorage.getItem('addId') !=null){
                $.ajax({
                    url: url() + 'api/goods/addressOne',    //请求的url地址
                    dataType: "JSON",   //返回格式为json
                    async: true,//请求是否异步，默认为异步，这也是ajax重要特性
                    type: "POST",   //请求方式
                    data:{
                        address_id:addId
                    },
                    success: function (data) {
                        // console.log(data.result)
                        //请求成功时处理
                        if(data.status==1){
                            $('#region').val(data.result.region);
                            $('input[name="name"]').val(data.result.consignee);
                            $('#listStreet').val(data.result.address);
                            $('input[name="ZipCode"]').val(data.result.zipcode);
                            $('input[name="mobile"]').val(data.result.mobile);
                        } else {
                            console.log(data.msg);
                        }
                    }
                });
			}
        });
	</script>
<script type="text/javascript">
        var on = false;
        ``;
        $('.check').on('click', function () {
            if (on == false) {
                $(this).addClass('checkOn');
                on = true
            } else {
                $(this).removeClass('checkOn');
                on = false
            }
        });

        var area1 = new LArea();
        area1.init({
            'trigger': '#region', //触发选择控件的文本框，同时选择完毕后name属性输出到该位置
            'valueTo': '#value1', //选择完毕后id属性输出到该位置
            'keys': {
                id: 'id',
                name: 'name'
            }, //绑定数据源相关字段 id对应valueTo的value属性输出 name对应trigger的value属性输出
            'type': 1, //数据源类型
            'data': LAreaData //数据源
        });
        area1.value = [1, 13, 3];//控制初始位置，注意：该方法并不会影响到input的value
        //确定并跳回
        function confirm()
        {
            var add = $('#value1').val();
            var nickname = $('input[name="name"]').val();
            var companyName = $('input[name="companyName"]').val();
            var listStreet = $('#listStreet').val();
            var ZipCode = $('input[name="ZipCode"]').val();
            var mobile = $.trim($('input[name="mobile"]').val());
            if(companyName == ''){
                alert("公司名称不能为空！");
                return false;
            }
            if(nickname == ''){
                alert("昵称不能为空！");
                return false;
            }
            //添加
            if (sessionStorage.getItem("addId") == null) {
                var addressId = '';
                if(add == ''){
                    alert("地区不能为空！");
                    return false;
                }
                var address = add.split(",");
                var province = address[0];
				var city = address[1];
				var district = address[2];
             //修改
            } else {
                if(add == ''){
                    province =   sessionStorage.getItem('province');
                    city =  sessionStorage.getItem('district');
                    district = sessionStorage.getItem('city');
                } else {
                    address = add.split(",");
                    province = address[0];
                    city = address[1];
                    district = address[2];
				}
                addressId = sessionStorage.getItem("addId")
            }

			if(listStreet == ''){
				alert("详细地址不能为空！");
				return false;
			} if(ZipCode == ''){
				alert("邮政编码不能空！");
				return false;
			}
			var myreg = /^1[3|4|5|7|8][0-9]{9}$/;
			if (mobile == '') {
				alert('手机号不能为空');
				return false;
			} else if (!myreg.test(mobile)) {
				alert('请输入正确的手机号');
				return false;
			}
			var user_id = sessionStorage.getItem('userId');
			$.ajax({
				url: url() + 'api/goods/addressSave', //请求的url地址
				dataType: "JSON", //返回格式为json
				async: true, //请求是否异步，默认为异步，这也是ajax重要特性
				type: "POST", //请求方式
				data:{
					addressId:addressId,
					user_id:user_id,
					consignee:nickname,
                    company_name:companyName,
					province:province,
					city:city,
					district:district,
					zipcode:ZipCode,
					address:listStreet,
					mobile:mobile,
				},
				success: function (data) {
					//请求成功时处理
					if(data.status == 1){
						alert(data.msg);
						window.location.href='invoiceAddress.html'
					} if (data.status == -1) {
						alert(data.msg);
					}
				}
			});
        }
    $(".back").click(function(){
		history.go(-1)
	});

</script>
</body>
</html>
